package com.woniuxy.shop.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.woniuxy.shop.dao.TopAddressDao;
import com.woniuxy.shop.entity.TopAddress;
import com.woniuxy.shop.exception.DaoException;
import com.woniuxy.shop.exception.ServiceException;;

/**
*@author 作者：
*version 创建时间：
*类说明：
*/
public class TopAddressService {
	TopAddressDao tad = new  TopAddressDao();
	public List<String> getAllTop(int id) {
		//查询指定ID的所有上层地址名字
		List<String> names = new ArrayList<>();
		try {
			TopAddress tas = tad.getTopByID(id);
			while(tas!=null) {
				names.add(tas.getName());
				tas = tad.getTopByID(tas.getParentid());
			}
			
		} catch (DaoException e) {
			new ServiceException("sql执行出错",e);
		}
		return names;
	}
	
	/**
	 * 根据名字和父ID获取Top
	 * @param name
	 * @param pid
	 * @return
	 */
	public TopAddress getTopAddressByName(String name, int pid) {
		TopAddress ta = null;
		try {
			ta = tad.getTopByNameAndId(name,pid);
		} catch (DaoException e) {
			new ServiceException("sql执行出错",e);
		}
		
		return ta;
	}

	public void add(TopAddress ta) {
		try {
			tad.add(ta);
		} catch (DaoException e) {
			new ServiceException("sql执行出错",e);
		}
		
	}
}
